package com.dailyblue.java.cloud.controller;

import com.dailyblue.cloud.bookstore.commons.util.JsonResult;
import com.dailyblue.cloud.bookstore.commons.util.ResultTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/demo")
public class DemoController {
    @Value("${server.port}")
    private Integer port;

    // demo
    @GetMapping
    public JsonResult a() {
        System.out.println(port);
        return ResultTool.success("aaa:" + port);
    }

    @GetMapping("/{name}")
    public JsonResult b(@PathVariable("name") String name) {
        System.out.println(port);
        return ResultTool.success(name + ":" + port);
    }

    @GetMapping("/c")
    public JsonResult c() throws Exception {
        System.out.println("c1");
        Thread.sleep(3000);
        System.out.println("c2");
        return ResultTool.success("ccc");
    }

    @GetMapping("/d")
    public JsonResult d() {
        System.out.println("d1");
        System.out.println("d2");
        return ResultTool.success("ddd");
    }

    @GetMapping("/e")
    public JsonResult e() throws Exception {
        System.out.println("e1");
        int num = (int) (Math.random() * 3);
        System.out.println("num:" + num);
        if (num == 0) {
            throw new RuntimeException();
        }
        System.out.println("e2");
        return ResultTool.success("eee");
    }
}
